using Assets.Scripts.GDK;
using DG.Tweening;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


namespace Brick
{
	public class BallFindWay : gdk_base_UI
	{
		public float boreSpeed = 0.2f;

		public int ballNum;

		public int addNum = 1;

		private bool bFast;

		private Vector2 curShootpos;

		private int showIndex = 3;

		public override void OnLoad()
		{
			gdk_game_manager.AddEventListener<int>(EventID.GAME_QUICKBG, OnQuick);
			gdk_game_manager.AddEventListener<Vector2>(EventID.GAME_BALL_FORCE, OnAddForce);
		}

		public override void UnLoad()
		{
			gdk_game_manager.RemoveEventListener<int>(EventID.GAME_QUICKBG, OnQuick);
			gdk_game_manager.AddEventListener<Vector2>(EventID.GAME_BALL_FORCE, OnAddForce);
		}

		private void OnAddForce(Vector2 pos)
		{
			addNum = 0;
			bFast = false;
		}

		private void OnQuick(int index)
		{
			showIndex = index;
		}

		public override void OnStart()
		{
			addNum = 0;
			GDK.ModuleManger.Module<BallFindWayModule>().SetWordShootPos(base.transform.position);
		}

		private void OnCount()
		{
			addNum++;
			int sendOut = GDK.ModuleManger.Module<BallBoxModule>().GetSendOut();
			if (!bFast && (double)((float)addNum * 1f / (float)sendOut) >= 0.8)
			{
				bFast = true;
				Time.timeScale = 2f;
				gdk_game_manager.DispachEvent(EventID.GAME_QUICKBG, 1);
			}
			List<int> list = new List<int>();
			list.Add(3);
			list.Add(addNum);
			gdk_game_manager.DispachEvent(EventID.GAME_BALL_NUM_CHANGE, list);
			if (addNum == GDK.ModuleManger.Module<BallBoxModule>().GetSendOut())
			{
				Time.timeScale = 1f;
				GDK.ModuleManger.Module<BallBoxModule>().SetBallBack(bBack: false);
				gdk_game_manager.DispachEvent(EventID.GAME_SHOOP_START);
				gdk_game_manager.DispachEvent(EventID.SHOW_PLAY_MENU);
				gdk_game_manager.DispachEvent(EventID.GAME_QUICKBG, 2);
			}
		}

		private void OnTriggerStay2D(Collider2D ball)
		{
			if (!ball.CompareTag("Ball"))
			{
				return;
			}
			ball.GetComponent<CircleCollider2D>().isTrigger = false;
			BallState state = ball.GetComponent<BallMove>().state;
			if (state != BallState.SLEEP && state != BallState.Bore && state != 0 && state == BallState.BACK)
			{
				Rigidbody2D component = ball.GetComponent<Rigidbody2D>();
				component.velocity = new Vector2(0f, 0f);
				if (!GDK.ModuleManger.Module<BallFindWayModule>().IsShoot())
				{
					curShootpos = ball.GetComponent<Transform>().position;
					Vector2 vector = base.transform.position;
					curShootpos.y = vector.y;
					ball.GetComponent<Transform>().position = curShootpos;
					GDK.ModuleManger.Module<BallFindWayModule>().SetWordShootPos(curShootpos);
					Vector2 shootPos = ball.GetComponent<Transform>().localPosition;
					GDK.ModuleManger.Module<BallFindWayModule>().SetShootPos(shootPos);
					GDK.ModuleManger.Module<BallFindWayModule>().SetShoot(haveMuzzel: true);
					ball.GetComponent<BallMove>().state = BallState.SLEEP;
					OnCount();
					gdk_game_manager.DispachEvent(EventID.GAME_BALL_POS_CHANGE, curShootpos);
				}
				else
				{
					StartCoroutine(MoveToMuzzle(ball.transform, curShootpos));
				}
			}
		}

		private void OnTriggerEnter2D(Collider2D ball)
		{
			if (!ball.CompareTag("Ball"))
			{
				return;
			}
			ball.GetComponent<CircleCollider2D>().isTrigger = false;
			if (ball.GetComponent<BallMove>().state != 0)
			{
				Rigidbody2D component = ball.GetComponent<Rigidbody2D>();
				component.velocity = new Vector2(0f, 0f);
				if (!GDK.ModuleManger.Module<BallFindWayModule>().IsShoot())
				{
					curShootpos = ball.GetComponent<Transform>().position;
					Vector2 vector = base.transform.position;
					curShootpos.y = vector.y;
					ball.GetComponent<Transform>().position = curShootpos;
					GDK.ModuleManger.Module<BallFindWayModule>().SetWordShootPos(curShootpos);
					Vector2 shootPos = ball.GetComponent<Transform>().localPosition;
					GDK.ModuleManger.Module<BallFindWayModule>().SetShootPos(shootPos);
					GDK.ModuleManger.Module<BallFindWayModule>().SetShoot(haveMuzzel: true);
					ball.GetComponent<BallMove>().state = BallState.SLEEP;
					OnCount();
					gdk_game_manager.DispachEvent(EventID.GAME_BALL_POS_CHANGE, curShootpos);
				}
				else
				{
					StartCoroutine(MoveToMuzzle(ball.transform, curShootpos));
				}
			}
		}

		private int getBallBackSpeed()
		{
			return GDK.ModuleManger.Module<BallBoxModule>().getBallBackSpeed();
		}

		private Vector2 getMuzzle()
		{
			return GDK.ModuleManger.Module<BallFindWayModule>().getShootPos();
		}

		public IEnumerator MoveToMuzzle(Transform ball, Vector2 muzzle)
		{
			ball.GetComponent<BallMove>().state = BallState.Bore;
			Vector2 rpos = ball.GetComponent<Rigidbody2D>().position;
			int speed = getBallBackSpeed();
			float dis = (rpos - muzzle).magnitude;
			DOTween.To(duration: dis / (float)speed, getter: () => rpos, setter: delegate (Vector2 x)
			{
				ball.GetComponent<Rigidbody2D>().position = x;
			}, endValue: muzzle).OnComplete(delegate
			{
				ball.GetComponent<CircleCollider2D>().isTrigger = false;
				ball.GetComponent<BallMove>().state = BallState.SLEEP;
				OnCount();
			});
			yield return new WaitForFixedUpdate();
		}
	}
}

